#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>

#include "debug.h"
#include "sdmng.h"
#include "record.h"
#include "content.h"

int cont_filter(const char *name,struct stat *ststat)
{
    if(name == NULL)
    {
	SLOG(ERR,"name is null!\n");
	return 0;
    }
    if(ststat == NULL)
    {
	SLOG(ERR,"content item [%s] stat is null!\n",name);
	return 0;
    }

    char *fname = strrchr(name,'/');
    int len,i;

    if(fname == NULL || (fname+1)==NULL)
	return 0;
    else 
	fname += 1;
    if(S_ISDIR(ststat->st_mode))
    {
	len = strlen("20000101");

	if(strlen(fname)!=len)
	    return 0;
	for(i=0;i<len;i++)
	{
	    if(fname[i]<'0' || fname[i]>'9')
		return 0;
	}
	char cur_rec_dir[128]={0};
	/// it's current record directory,don't delete it if we have other choice
//	rsc_get_cur_recdir(cur_rec_dir,sizeof(cur_rec_dir));
	if(!strcmp(name,cur_rec_dir))
	    return 0;
	return 1;
    }
    else if(S_ISREG(ststat->st_mode))
    {/// video:avi/mav,audio:wav/mav,image:jpg
#ifdef CONFIG_RECORD_MAV
	len = strlen("141414.mav");
#else
	len = strlen("141414.avi");
#endif

	if(strlen(fname)!=len)
	    return 0;
	for(i=0;i<6;i++)
	{
	    if(fname[i]<'0' || fname[i]>'9')
		return 0;
	}
	return 1;
    }

    return 0;
}

int cont_delter(const char *name,struct stat *ststat)
{
    if(name == NULL)
    {
	SLOG(ERR,"name is null!\n");
	return -1;
    }

    int ret = -1;// default should be FAILED
    char *fname = strrchr(name,'/');

    //SLOG(PRI,"name = %s,fname = %s\n",name,fname);
    if(fname == NULL || (fname+1)==NULL)
	return -1;
    else 
	fname += 1;

    ret = remove(name);
    if (ret) 
    {
	SLOG(ERR, "remove %s err!\n", name);
    } 
    else
    {
	SLOG(PRI,"remove file [%s] ok!\n",name);
    }
    return ret;
}

